package jeons.review.action;



import java.text.ParseException;

import java.util.Date;

import jeons.common.util.PageDTO;
import jeons.common.util.Utils;

import jeons.common.util.PageingHelpers;
import jeons.review.biz.ReviewBiz;
import jeons.review.biz.ReviewReplyBiz;
import jeons.review.dto.ReviewDTO;
import jeons.review.dto.ReviewReplyDTO;

import org.springbyexample.web.servlet.view.tiles2.TilesUrlBasedViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller
@RequestMapping("/review")
public class ReviewAction {
	@Autowired TilesUrlBasedViewResolver tilesUrlBasedViewResolver;
	@Autowired ReviewBiz reviewBiz;
	@Autowired ReviewReplyBiz reviewReplyBiz;
	
	/**
	* @Method Name  : index
	* @�ۼ���   : 2012. 2. 21.
	* @�ۼ���   : user
	* @�����̷�  :
	* @Method ���� : ��ǰ�� �ε���
	* @param model
	* @param pageDTO
	* @param reviewDTO
	* @return
	*/
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(Model model, PageDTO pageDTO,@RequestParam("bdno")int bdno,@ModelAttribute("reviewDTO")ReviewDTO reviewDTO){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		reviewDTO.setBdno(bdno);
		int count = reviewBiz.getCount();
		model.addAttribute("count", count);
		model.addAttribute("reviewDTO",reviewBiz.getList(reviewDTO));
		model.addAttribute("pagereview",PageingHelpers.instance.autoPaging(count, pageDTO.getPlRowRange(), pageDTO.getPlPageRange(), reviewDTO.getPage()));
		return "review/index";
	}
	
	/**
	* @Method Name  : insert
	* @�ۼ���   : 2012. 2. 21.
	* @�ۼ���   : user
	* @�����̷�  :
	* @Method ���� : �μ�Ʈ �� ������Ʈ ������
	* @param model
	* @param reno
	* @return
	*/
	@RequestMapping(value="/insert", method=RequestMethod.GET)
	public String insert(Model model,@RequestParam(value="reno" , required=false, defaultValue="0")int reno){
		if (reno == 0) {
			model.addAttribute("reviewDTO", new ReviewDTO());
		}else{
			model.addAttribute("reviewDTO", reviewBiz.getData(reno));
		}
		
		return "review/insert";
	}
	
	/**
	* @Method Name  : insertData
	* @�ۼ���   : 2012. 2. 21.
	* @�ۼ���   : user
	* @�����̷�  :
	* @Method ���� :  �μ�Ʈ �� ������Ʈ ó��
	* @param reviewDTO
	* @return
	*/
	@RequestMapping(value="/insert", method=RequestMethod.POST)
	public void insertData(@ModelAttribute("reviewDTO")ReviewDTO reviewDTO){
		if (reviewDTO.getReno() == null) {
			reviewBiz.insertData(reviewDTO);
		}else{
			reviewBiz.updateData(reviewDTO);
		}
		/*return "redirect:review/index/";*/
	}
	
	
	/**
	* @Method Name  : viewData
	* @�ۼ���   : 2012. 2. 21.
	* @�ۼ���   : user
	* @�����̷�  :
	* @Method ���� : ������ ����
	* @param model
	* @param reno
	* @return
	*/
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String viewData(Model model,@RequestParam("reno")int reno){
		
		model.addAttribute("data", reviewBiz.getData(reno));
		model.addAttribute("data_reply", reviewReplyBiz.getList(reno));
		model.addAttribute("re_reply", new ReviewReplyDTO());
		return "review/view";
	}
	
	
	/**
	* @Method Name  : deleteData
	* @�ۼ���   : 2012. 2. 21.
	* @�ۼ���   : user
	* @�����̷�  :
	* @Method ���� : ����
	* @param reno
	* @return
	*/
	@RequestMapping(value="/delete" , method=RequestMethod.GET)
	public @ResponseBody String deleteData(@RequestParam("reno")int reno){
		String check = "true";
		try {
			reviewBiz.deleteData(reno);
			reviewReplyBiz.deleteALLData(reno);
		} catch (Exception e) {
			check = "false";
		}
		return check;
	}
	
	/**유저 비번 가져오기
	 * @param model
	 * @param reno
	 * @param board_type
	 * @return
	 */
	@RequestMapping(value="/checkpasswd", method=RequestMethod.GET)
	public String checkpasswd(Model model,@RequestParam("reno")int reno, @RequestParam("board_type")String board_type){
		model.addAttribute("board_type", board_type);
		model.addAttribute("reno", reno);
		model.addAttribute("userpasswd",reviewBiz.getUserPasswd(reno));
		return "review/checkpasswd";
	}
	
	//리플저장
	@RequestMapping(value="/replyInsert", method=RequestMethod.POST)
	public @ResponseBody ReviewReplyDTO replyInsertData(@ModelAttribute("re_reply")ReviewReplyDTO replyDTO) throws ParseException{
		System.out.println("replyDTO.getContent()  : \t"+replyDTO.getContent());
		Date tmp = Utils.formatDate("yyyy-MM-dd HH:mm:ss");
		System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa tmp : "+tmp);
		String stringdate  = Utils.formDataString(tmp);
		System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBB stringdate" + stringdate);
		replyDTO.setRegdt(tmp);
		ReviewReplyDTO reply = reviewReplyBiz.insertData(replyDTO);
		System.out.println("*********************************");
		System.out.println(reply.getRe_reno());
		System.out.println(reply.getContent());
		reply.setStringdate(stringdate);
		return reply;
	}
	
	//리플지우기
	@RequestMapping(value="/replyDelete", method=RequestMethod.POST)
	public @ResponseBody String replayDelete(@RequestParam("re_reno")int re_reno){
		String check = "true";
		try {
			reviewReplyBiz.deleteData(re_reno);
		} catch (Exception e) {
			// TODO: handle exception
			check = "false";
		}
		return check;
	}
}
